/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.mrp.ppm.entity;

import java.io.Serializable;
import java.text.DecimalFormat;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import merlion.common.util.Consts;
import merlion.scm.icm.entity.ProductInventoryRecord;

/**
 *
 * @author Yuwei and ZX
 */
@Entity
public class Product implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String productType;
    private Double basePrice;
    private Double wsMarkup;
    private Double dsMarkup;
    private Double wsUnitPrice; //for wholesales    
    private Double dsUnitPrice; //for direct sales
    private String description;
    @OneToOne(targetEntity=ProductInventoryRecord.class, cascade={CascadeType.PERSIST,CascadeType.MERGE}, fetch=FetchType.EAGER)
    private ProductInventoryRecord pir;
    
    public Product() {
        id = (long) 0;
        productType = "";
        basePrice = (double) 0;
        wsMarkup = (double) 0;
        dsMarkup = (double) 0;
        wsUnitPrice = basePrice * wsMarkup;
        dsUnitPrice = basePrice * dsMarkup;
        description = "";
        pir = null;
    }
    
    public Product(int type, Double basePrice) {
        switch (type) {
            case 0:
                productType = Consts.ProductType.BAR_A.getText() + "";
                break;
            case 1:
                productType = Consts.ProductType.BAR_B.getText() + "";
                break;
            case 2:
                productType = Consts.ProductType.BAR_C.getText() + "";
                break;
            case 3:
                productType = Consts.ProductType.BAR_D.getText() + "";
                break;
            default:
                break;
        }
                this.basePrice = basePrice;
                wsMarkup = (double) 1.5;
                dsMarkup = (double) 2.0;
                wsUnitPrice = basePrice * wsMarkup;
                dsUnitPrice = basePrice * dsMarkup;
    
    }

    public Product(int type) {
        switch (type) {
            case 0:
                productType = Consts.ProductType.BAR_A.getText() + "";
                basePrice = (double) 0.5;
                wsMarkup = (double) 1.5;
                dsMarkup = (double) 2.0;
                wsUnitPrice = basePrice * wsMarkup;
                dsUnitPrice = basePrice * dsMarkup;
                break;
            case 1:
                productType = Consts.ProductType.BAR_B.getText() + "";
                basePrice = (double) 0.6;
                wsMarkup = (double) 1.5;
                dsMarkup = (double) 2.0;
                wsUnitPrice = basePrice * wsMarkup;
                dsUnitPrice = basePrice * dsMarkup;
                break;
            case 2:
                productType = Consts.ProductType.BAR_C.getText() + "";
                basePrice = (double) 0.8;
                wsMarkup = (double) 1.5;
                dsMarkup = (double) 2.0;
                wsUnitPrice = basePrice * wsMarkup;
                dsUnitPrice = basePrice * dsMarkup;
                break;
            case 3:
                productType = Consts.ProductType.BAR_D.getText() + "";
                basePrice = (double) 1;
                wsMarkup = (double) 1.5;
                dsMarkup = (double) 2.0;
                wsUnitPrice = basePrice * wsMarkup;
                dsUnitPrice = basePrice * dsMarkup;
                break;
            default:
                break;
        }

    }
    
    private double roundToFourDecimals(double d){
        DecimalFormat fourDForm = new DecimalFormat("#.####");
        return Double.valueOf(fourDForm.format(d));
    }

    public void create(String productType, Double basePrice, Double wsMarkup, Double dsMarkup) {
        this.setProductType(productType);
        this.setBasePrice(basePrice);
        this.setWsMarkup(wsMarkup);
        this.setDsMarkup(dsMarkup);
        this.setWsUnitPrice(basePrice * wsMarkup);
        this.setDsUnitPrice(basePrice * dsMarkup);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the productType
     */
    public String getProductType() {
        return productType;
    }

    /**
     * @param productType the productType to set
     */
    public void setProductType(String productType) {
        this.productType = productType;
    }

    public Double getBasePrice() {
        return basePrice;
    }

    public void setBasePrice(Double basePrice) {
        this.basePrice = basePrice;
    }

    public Double getDsUnitPrice() {
        return dsUnitPrice;
    }

    public void setDsUnitPrice(Double dsUnitPrice) {
        this.dsUnitPrice = dsUnitPrice;
    }

    public Double getWsUnitPrice() {
        return wsUnitPrice;
    }

    public void setWsUnitPrice(Double wsUnitPrice) {
        this.wsUnitPrice = wsUnitPrice;
    }

    public Double getDsMarkup() {
        return dsMarkup;
    }

    public void setDsMarkup(Double dsMarkup) {
        this.dsMarkup = dsMarkup;
    }

    public Double getWsMarkup() {
        return wsMarkup;
    }

    public void setWsMarkup(Double wsMarkup) {
        this.wsMarkup = wsMarkup;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public ProductInventoryRecord getPir() {
        return pir;
    }

    public void setPir(ProductInventoryRecord pir) {
        this.pir = pir;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Product)) {
            return false;
        }
        Product other = (Product) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.crm.slcm.entity.Product[ id=" + id + " ]";
    }
}
